package com.frxuexi.mynews.repository

import com.frxuexi.lib_commom.RetrofitManager
import com.frxuexi.mynews.ApiService
import com.frxuexi.mynews.entity.BaseEntity
import com.frxuexi.mynews.entity.CommentEntity
import com.frxuexi.mynews.entity.NewsDataEntity
import com.frxuexi.mynews.entity.NewsDetailEntity
import com.frxuexi.mynews.entity.NewsTypeEntity
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn
import okhttp3.RequestBody
import retrofit2.http.Body
import retrofit2.http.Query

class NewsTypeRepository {
    val apiService by lazy { RetrofitManager.getRetrofit().create(ApiService::class.java) }
    fun getNewsType():Flow<BaseEntity<List<NewsTypeEntity>>>{
            val flow = apiService.getNewsType()
                .flowOn(Dispatchers.IO)
            return flow

    }

    fun getNews(page:Int,type:Int):Flow<BaseEntity<NewsDataEntity>>{
        val flow = apiService.getNews(page, type)
            .flowOn(Dispatchers.IO)
        return flow
    }

    fun getNewsDetail(id:Int):Flow<BaseEntity<NewsDetailEntity>>{
        val flow = apiService.getNewsDetail(id)
            .flowOn(Dispatchers.IO)
        return flow
    }
    //收藏
    fun newsCollect(nid:Int):Flow<BaseEntity<String>>{
        val flow = apiService.newsCollect(nid)
            .flowOn(Dispatchers.IO)
        return flow
    }

    //查询我的收藏，这条数据是否收藏过，根据结果显示收藏图标
    fun myCollect():Flow<BaseEntity<List<NewsDetailEntity>>>{
        val flow = apiService.myCollect()
            .flowOn(Dispatchers.IO)
        return flow
    }

    //发布评论
    fun newsComment(body: RequestBody):Flow<BaseEntity<String>>{
        val flow = apiService.newsComment(body)
            .flowOn(Dispatchers.IO)
        return flow
    }

    //根基新闻id获取评论
    fun getCommentsById(nid: Int):Flow<BaseEntity<List<CommentEntity>>>{
        val flow = apiService.getCommentsById(nid)
            .flowOn(Dispatchers.IO)
        return flow
    }
}